//复制文字方法
//content  文字内容
function copyToClipboard(content) {
	// 创建元素用于复制
	var aux = document.createElement("input");
	// 设置元素内容
	aux.setAttribute("value", content);
	// 将元素插入页面进行调用
	document.body.appendChild(aux);
	// 复制内容
	aux.select();
	// 将内容复制到剪贴板
	document.execCommand("copy");
	// 删除创建元素
	document.body.removeChild(aux); 	
}

//图片下载
//imgsrc 图片路径 
//name  新图片命名
function imageDownload(imgsrc,name){
	let image = new Image();
	// 解决跨域 Canvas 污染问题
	image.setAttribute("crossOrigin", "anonymous");
	image.onload = function() {
		let canvas = document.createElement("canvas");
		canvas.width = image.width;
		canvas.height = image.height;
		let context = canvas.getContext("2d");
		context.drawImage(image, 0, 0, image.width, image.height);
		let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
		let a = document.createElement("a"); // 生成一个a元素
		let event = new MouseEvent("click"); // 创建一个单击事件
		a.download = name || "photo"; // 设置图片名称
		a.href = url; // 将生成的URL设置为a.href属性
		a.dispatchEvent(event); // 触发a的单击事件
	};
	image.src = imgsrc;
}

//休眠函数
//numberMillis 休眠时间 
function sleep(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
		return;
    }
}

//根据字符串获取时间戳
function tranStrToTimestamps(str = ''){
	timestamps = (new Date(str)).getTime()/1000;
	return timestamps;
}


//获取几天前时间戳
function getValidTime(dayCount = 1){
	curDate = new Date();
	var preDate = new Date(curDate.getTime() - dayCount*24*60*60*1000);
	var year = preDate.getFullYear();
	var month = preDate.getMonth();
	var day = preDate.getDate();
	var time = year + '-' + (month+1) + '-' + day;
	var dayStart = Date.parse(new Date(time)); 
	return dayStart;
}

//获取几天前时间日期
function getValidDate(dayCount = 1){
	curDate = new Date();
	var preDate = new Date(curDate.getTime() - dayCount*24*60*60*1000);
	
	var year = preDate.getFullYear();
	var month = preDate.getMonth()+1;
	if(month < 10){
		month = '0'+ month;
	}
	var day = preDate.getDate();
	if(day < 10){
		day = '0'+ day;
	}
	var time = year + '-' + month + '-' + day;
	return time;
}

//获取时间戳日期
function getTimeDate(new_timestamps){
	var preDate = new Date(new_timestamps);
	
	var year = preDate.getFullYear();
	var month = preDate.getMonth()+1;
	if(month < 10){
		month = '0'+ month;
	}
	var day = preDate.getDate();
	if(day < 10){
		day = '0'+ day;
	}
	var time = year + '-' + month + '-' + day;
	
	return time;
}
//获取当前年月日 时分秒
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 + date.getMinutes()
            + seperator2 + date.getSeconds();
    return currentdate;
}

function getRangeDate(start_date,end_date){
    start_time = Date.parse(new Date(start_date));
	end_time = Date.parse(new Date(end_date));
	date_arr = [];
	for(let i = 0;i<1000;i++){
		cur_time = end_time - i*24*60*60*1000;
		if(cur_time >= start_time){
			cur_date = getTimeDate(cur_time);
			date_arr.push(cur_date);
		}else{
			break;
		}
	}
	return date_arr;
}

//去掉所有的html标记
function delHtmlTag(str)
{
　　return str.replace(/<[^>]+>/g,"");
}


//服务端存缓存 /api/daorc/product/cache
function setCache(key,val){
	let url = 'https://ydnr.youdnr.com/api/daorc/product/cache';
	result = '';
	$.ajax({
		url:url,
		type:"post",
		async:false,
	    data:{
			key:key,
			val:val,
			type:"set"
		},
		success:function(data){
			if(data){
				result = true;
			}
		},
	})
	return result;
}

//服务端存取缓存 /api/daorc/product/cache
function getCache(key,val){
	let url = 'https://ydnr.youdnr.com/api/daorc/product/cache';
	result = '';
	$.ajax({
		url:url,
		type:"post",
		async:false,
	    data:{
			key:key,
			type:"get"
		},
		success:function(data){
			if(data){
				result = data;
			}
		},
	})
	return result;
}
